const express = require("express");
const router = express.Router();
const passport = require('passport');
const Package = require("../../models/Package");
const pagination = require('mongoose-sex-page')


router.get('/package/list', passport.authenticate("jwt", { session: false }), (req, res) => {
  let packageFields = {}
  if (req.query.models) packageFields.models = req.query.models
  if (req.query.price) packageFields.price = req.query.price
  if (req.query.desc) packageFields.desc = req.query.desc
  if (req.query.startTime) packageFields.count = req.query.startTime
  if (req.query.endTime) packageFields.count = req.query.endTime

  // page 当前页
  pagination(Package).find(packageFields).page(req.query.pageNum).size(req.query.pageSize).exec()
    .then(package => {
      if (!package) {
        return res.status(400).json({ status: 400, message: '获取用户列表失败', data: [] })
      }
      const total = package.length;
      return res.json({ status: 200, message: "请求成功", success: true, data: package, total })
    })
    .catch(error => {
      return res.status(400).json({ status: 400, error, message: "获取列表失败" })
    })
})

router.post('/package/add', passport.authenticate('jwt', { session: false }), (req, res) => {
  let packageField = {}
  if (req.body.models) packageField.models = req.body.models
  if (req.body.price) packageField.price = req.body.price
  if (req.body.desc) packageField.desc = req.body.desc
  if (req.body.startTime) packageField.startTime = req.body.startTime
  if (req.body.endTime) packageField.endTime = req.body.endTime
  new Package(packageField).save()
    .then(() => {
      // package.save().then(package => res.json(package));
      return res.json({ status: 200, success: true, message: '添加成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '添加失败!' })
    });
});

router.post('/package/edit/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  let packageField = {}
  if (req.body.models) packageField.models = req.body.models
  if (req.body.price) packageField.price = req.body.price
  if (req.body.desc) packageField.desc = req.body.desc
  if (req.body.startTime) packageField.startTime = req.body.startTime
  if (req.body.endTime) packageField.endTime = req.body.endTime
  Package.findOneAndUpdate(
    { _id: req.params.id },
    { $set: packageField },
    { new: true })
    .then(() => {
      // package.save().then(package => res.json(package));
      return res.json({ status: 200, success: true, message: '修改成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '修改失败!' })
    });
});



router.delete('/package/delete/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  Package.findOneAndDelete({ _id: req.params.id })
    .then(package => {
      // package.save().then(package => res.json(package));
      return res.json({ status: 200, success: true, message: '删除成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '删除失败!' })
    });
});


router.get('/package/detail/:id', passport.authenticate("jwt", { session: false }), (req, res) => {
  Package.findOne({ _id: req.params.id })
    .then(package => {
      return res.json({ status: 200, message: "请求成功", success: true, data: package })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, message: " 获取信息失败", error })
    })
})

module.exports = router
